import { app, BrowserWindow, ipcMain } from 'electron'
import { electronApp, optimizer } from '@electron-toolkit/utils'
import MainFrame from './Frame/MainFrame'
import Routes from './router/Router.template'
import Router from './router/Router'

/** 主窗口 */
const mainFrame = new MainFrame()

app.whenReady().then(() => {
  electronApp.setAppUserModelId('com.electron')

  app.on('browser-window-created', (_, window) => {
    optimizer.watchWindowShortcuts(window)
  })

  /***
   * 窗口创建
   */
  mainFrame.create()

  /**
   * 事件
   */
  const router = new Router()
  // 添加窗口API
  router.addFrame('mainFrame', mainFrame)
  // 添加事件
  router.addRoute(Routes)

  /** 监听事件 */
  ipcMain.handle('render-to-main', (_, data) => {
    router.trigger(data)
  })

  app.on('activate', function () {
    if (BrowserWindow.getAllWindows().length === 0) mainFrame.create()
  })
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})
